//
//  EdHttp.m
//  短信
//
//  Created by xiaocao on 2018/5/29.
//  Copyright © 2018年 Edubrain. All rights reserved.
//

#import "EdHttp.h"

#import "AppDelegate.h"
#import <AFNetworking.h>
@implementation EdHttp
+ (void)get:(NSString *)url
     params:(id)params
   progress:(void (^)(id downloadProgress))progress
    success:(void (^)(id responseObject))success
    failure:(void (^)(NSError *error))failure
{
    
    // 1.创建一个请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    AFJSONResponseSerializer *jsonResponseS = (AFJSONResponseSerializer *)mgr.responseSerializer;
    jsonResponseS.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    // 2.发送一个GET请求
    [mgr GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        progress(downloadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([[responseObject objectForKey:@"code"] integerValue] == 300) {
            
            return ;
        }
        if (success) {
            
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            
            failure(error);
        }
    }];
}
+ (void)post:(NSString *)url
      params:(id)params
    progress:(void (^)(id))progress
     success:(void (^)(id))success
     failure:(void (^)(NSError *))failure
{
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    // 1.创建一个请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    AFJSONResponseSerializer *jsonResponseS = (AFJSONResponseSerializer *)mgr.responseSerializer;
    jsonResponseS.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    // 2.发送一个POST请求
    [mgr POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        progress(uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        if ([[responseObject objectForKey:@"code"] integerValue] == 300) {
            
            
            return ;
        }
        if (success) {
            
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            
            failure(error);
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        }
    }];
}


+ (void)upLoad:(NSString *)url
        params:(id)params
         files:(NSArray *)files
      progress:(void (^)(id))progress
       success:(void (^)(id))success
       failure:(void (^)(NSError *))failure
{
    
    // 1.创建一个请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    AFJSONResponseSerializer *jsonResponseS = (AFJSONResponseSerializer *)mgr.responseSerializer;
    jsonResponseS.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    // 2.发送请求
    [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (HttpFile *file in files) {
            [formData appendPartWithFileData:file.data name:file.name fileName:file.filename mimeType:file.mimeType];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        progress(uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            
            failure(error);
        }
    }];
}
@end

@implementation HttpFile

+ (instancetype)fileWithName:(NSString *)name
                        data:(NSData *)data
                    mimeType:(NSString *)mimeType
                    filename:(NSString *)filename
{
    HttpFile *file = [[self alloc] init];
    file.name = name;
    file.data = data;
    file.mimeType = mimeType;
    file.filename = filename;
    return file;
}
@end

